const { logger } = require('../utils');
const { knex, TABLE_NAME } = require('./db');

knex.schema.hasTable(TABLE_NAME.worker).then((exists) => {
  if (!exists) {
    return knex.schema.createTable(TABLE_NAME.worker, (t) => {
      t.charset('utf8');
      t.uuid('id').primary();
      t.string('name', 100);
      t.string('mobile', 50);
      t.string('gender');
      t.integer('age', 4);
      t.string('education');
      t.string('idcardNo');
      t.string('birthPlace');
      t.string('address');
      t.string('creatorOpenId', 50);
      t.string('organizationId', 50);
      t.timestamp('createdOn').defaultTo(knex.fn.now());
      t.string('status');
      t.string('proposalStatus');
      t.string('contractId');
      t.foreign('contractId').references(`${TABLE_NAME.contract}.id`)
        .onDelete('SET NULL');
      t.string('contractName');
      t.string('contractStatus');
      t.string('deadline');
      t.timestamp('endTime');
      t.timestamp('startTime');
    });
  }
})
  .catch((err) => {
    logger.error({
      message: err.message,
      stack: err.stack,
      Action: 'worker/createTable',
    });
    console.error(err);
  });

